﻿/* 
Copyright 2008 William C. Pierce 

Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

	http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
*/

using System;
using System.Collections;
using System.Reflection;
using System.Security;
using Iesi.Collections.Generic;
using log4net;
using NHibernate;
using NHibernate.Bytecode;
using NHibernate.Engine;
using NHibernate.Proxy;
using NHibernate.Proxy.Poco.Castle;
using NHibernate.Type;
using IInterceptor=Castle.Core.Interceptor.IInterceptor;

//[assembly: AssemblyVersion("{VERSION}")]
//[assembly: AllowPartiallyTrustedCallers]

public class StaticProxyFactory : IProxyFactory
{
	private static readonly ILog _log;
	private static readonly IDictionary _proxies;

	private string _entityName;
	private Type _persistentClass;
	private Type[] _interfaces;
	private MethodInfo _getIdentifierMethod;
	private MethodInfo _setIdentifierMethod;
	private IAbstractComponentType _componentIdType;
	private bool _isClassProxy;
	private string _proxyKey;
	private Type _proxyType;

	static StaticProxyFactory()
	{
		_log = LogManager.GetLogger( typeof( StaticProxyFactory ) );
		_proxies = new Hashtable(); 
//{PROXIES}
	}

	public void PostInstantiate(string entityName, Type persistentClass, ISet<Type> interfaces, MethodInfo getIdentifierMethod, MethodInfo setIdentifierMethod, IAbstractComponentType componentIdType)
	{
		_entityName = entityName;
		_persistentClass = persistentClass;
		_interfaces = new Type[interfaces.Count];
		interfaces.CopyTo( _interfaces, 0 );
		_getIdentifierMethod = getIdentifierMethod;
		_setIdentifierMethod = setIdentifierMethod;
		_componentIdType = componentIdType;
		_isClassProxy = _interfaces.Length == 1;

		if( _isClassProxy )
		{
			_proxyKey = _persistentClass.FullName;
			_log.Debug( _persistentClass.FullName );
		}
		else
		{
			_proxyKey = _interfaces[0].FullName;
			_log.Debug( _interfaces[0].FullName );
		}

		foreach( Type iface in _interfaces )
		{
			_log.Debug( iface.FullName );
			_proxyKey = string.Concat( _proxyKey, "|", iface.FullName );
		}
		_log.Debug( _proxyKey );

		_proxyType = _proxies[_proxyKey] as Type;
	}

	public INHibernateProxy GetProxy(object id, ISessionImplementor session)
	{
		INHibernateProxy proxy;
		try
		{
			object generatedProxy;
			CastleLazyInitializer initializer = new CastleLazyInitializer( _entityName, _persistentClass, id, _getIdentifierMethod, _setIdentifierMethod, _componentIdType, session );
			IInterceptor[] interceptors = new IInterceptor[] { initializer };

			object[] args;
			if( _isClassProxy )
			{
				args = new object[] { interceptors }; 
			}
			else
			{
				args = new object[] { interceptors, new object() };
			}
			generatedProxy = Activator.CreateInstance( _proxyType, args ); 

			initializer._constructed = true;
			proxy = (INHibernateProxy)generatedProxy;
		}
		catch(Exception e)
		{
			string message = "Creating a proxy instance failed";
			_log.Error( message, e );
			throw new HibernateException( message, e );	
		}
			
		return proxy;
	}
}

public class StaticProxyFactoryFactory : IProxyFactoryFactory
{
	public IProxyFactory BuildProxyFactory()
	{
		return new StaticProxyFactory();
	}
}